package com.yzh.springboot_rbac.service.impl;

import com.yzh.springboot_rbac.entity.Dept;
import com.yzh.springboot_rbac.entity.User;
import com.yzh.springboot_rbac.mapper.UserMapper;
import com.yzh.springboot_rbac.mapper.UserPostMapper;
import com.yzh.springboot_rbac.mapper.UserRoleMapper;
import com.yzh.springboot_rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserPostMapper userPostMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public List<User> selectUserBydeptName(Dept dept) {
        return userMapper.selectUserBydeptName(dept);
    }

    @Override
    public List<User> selectUserByCondition(User user) {
        return userMapper.selectUserByCondition(user);
    }

    /*1.插入用户，更新用户岗位表和用户角色表*/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean isAddUser(User user, Integer postId, Integer roleId) throws Exception {
        List<User> userList = userMapper.selectUserByCondition(user);
        if (userList.isEmpty()) {
            int row = userMapper.insertUser(user);
            if (row > 0) {
                Integer userId = user.getUserId();
                if (userId == null) {
                    throw new Exception("插入用户成功，但未获取到 userId");
                }
//                log.info("插入用户成功，生成的 userId: {}", userId); // 打印 userId

                int i = userPostMapper.insertUserPostId(userId, postId);
                if (i <= 0) {
                    throw new Exception("插入用户岗位关系表失败，触发回滚");
                }

                int j = userRoleMapper.insertUserRoleId(userId, roleId);
                if (j <= 0) {
                    throw new Exception("插入用户角色关系表失败，触发回滚");
                }
                return true;
            } else {
                throw new Exception("插入用户失败");
            }
        } else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//保证所有sql要么都成功，要么都回滚
    public Boolean isUpdateUser(User user, Integer postId, Integer roleId) throws Exception {
        int row = userMapper.updateUser(user);
        Integer userId = user.getUserId();
        if (row > 0) {
            int i = userPostMapper.updateUserPostId(userId, postId);

            /*这个地方注意，原来我自己写的有问题，执行关系表的插入和更新语句不用return，要不然if--else后面无法执行更新语句了
             * 这直接两种情况包圆了。*/
            if (i <= 0) {
                throw new Exception("更新用户岗位关系表错误");
            }
            int j = userRoleMapper.updateUserRoleId(userId, roleId);
            if (j <= 0) {
                throw new Exception("更新用户角色表错误");
            }
            return true;
        } else {
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteUser(User user) {
        int row = userMapper.deleteUser(user);
        if (row > 0){
            userPostMapper.deleteUserPostId(user);
            userRoleMapper.deleteUserRoleId(user);
            return true;
        } else {
            return false;
        }
    }
}